package com.groups.service;

import java.util.List;

import com.groups.common.SPageBean;
import com.groups.dao.ICommonDAO;
import com.groups.entity.Good;

public class AllGoodService {
	private ICommonDAO commonDao;

	public ICommonDAO getCommonDao() {
		return commonDao;
	}

	public void setCommonDao(ICommonDAO commonDao) {
		this.commonDao = commonDao;
	}
	public List getAllGoodsByPage(SPageBean spb){
		String hql = "from Good g";
		commonDao.listByPage(hql, spb);
		return (List)spb.getDatas();
	}
	public Good checkGood(Good g){
		String hql = "from Good";
		List<Good> goods = commonDao.list(hql);
		if(goods==null||goods.size()==0){
			return null;
		}
		return goods.get(0);
	}
	public boolean addGood(Good g){
		boolean flag = false;
		try{
			commonDao.add(g);
			flag = true;
		}
		catch(Exception e){
			flag = false;
		}
		return flag;
	}
	public Object[] getGoodDetail(int goodId){
		String hql = "from Order o inner join fetch o.good where o.good.goodId=?";
		Object[] args = new Object[]{goodId};
		List list = commonDao.list(hql, args);
		int count = list.size();
		//Order order = (Order)list.get(0);
		String hql1 = "from Good g where g.goodId = ?";
		List list1 = commonDao.list(hql1,args);
		Good g = (Good) list1.get(0);
		Object[] o = new Object[]{g.getGoodName(),g.getGoodOldprice(),g.getGoodSave(),g.getGoodPrice(),
				g.getGoodDiscount(),g.getStartTime(),g.getEndTime(),
				g.getGoodInfo(),g.getGoodUrl(),g.getGoodkind(),count};
		return o;
	}
	public Good getGoodById(int id){
		Good good = (Good)commonDao.get(Good.class, id);
		return good;
	}
	
	public boolean updateGood(Good good){
		boolean flag = false;
		try{
			commonDao.update(good);
			flag = true;
		}
		catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
	public boolean deleteGood(int goodid){
		boolean flag = false;
		try{
			commonDao.delete(Good.class,goodid);
			flag = true;
		}
		catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
	public List<Good> QueryGood(SPageBean spb,Good good){
		String hql = "from Good where goodName like '%"+good.getGoodName()+"%'";
		commonDao.listByPage(hql, spb);	
		return (List<Good>)spb.getDatas();
	}
}
